//
//  Help.m
//  qulvyou
//
//  Created by mmyy on 15/10/23.
//  Copyright (c) 2015年 mmyy. All rights reserved.
//

#import "Help.h"

@implementation Help

+ (NSString *)pathWithBasicPath:(NSString *)basicPath andDic:(NSDictionary *)dic {
    
    if (dic == nil) {
        return basicPath;
    }
    
    NSMutableArray *keyValueArr = [NSMutableArray array];
    for (NSString *key in dic.keyEnumerator) {
        
        NSLog(@"%@  %@",key, dic[key]);
        NSString *keyAndValue = [NSString stringWithFormat:@"%@=%@",key, dic[key]];
        NSLog(@"%@",keyAndValue);
        [keyValueArr addObject:keyAndValue];
        
    }
    
    //    client_id=3601604349&edirect_uri=http://hi.baidu.com/jt_one&display=mobile&response_type=token
    NSString *path = [keyValueArr componentsJoinedByString:@"&"];
    
    path = [NSString stringWithFormat:@"%@?%@",basicPath,path];
    
    return path;
}



+ (NSString *)getValueWithInfo:(NSString *)infoStr andKey:(NSString *)key {
    //access_token=2.008kmTQDjoxjvD3525f332a9tVOX9D&remind_in=494237&expires_in=494237&uid=2991918775
    
    //    access_token=   remind_in=  expires_in=  uid=
    //    494237&uid=2991918775
    //    494237&expires_in=494237&uid=2991918775
    
    //    1.应该先搜索key的范围，｛location，length｝
    NSRange keyRange = [infoStr rangeOfString:key];
    
    //    2.location ＋ length
    NSInteger valueLocation = keyRange.location + keyRange.length;
    
    //    3.从2中的位置开始切割到最后，valueStr
    //    2.008kmTQDjoxjvD3525f332a9tVOX9D&remind_in=493025&expires_in=493025&uid=2991918775
    NSString *valueStr = [infoStr substringFromIndex:valueLocation];
    
    
    //    4.搜索valueStr中第一个&符号的位置
    NSRange range = [valueStr rangeOfString:@"&"];
    
    if (range.location == NSNotFound) {
        return valueStr;
    }
    
    
    //    5.直接切割到&符号的位置就行
    NSInteger location = range.location;
    
    NSString *value = [valueStr substringToIndex:location];
    
    return value;
}


+ (BOOL)isExpireIn {
    
    NSDate *futherDate = [[NSUserDefaults standardUserDefaults] objectForKey:EXPIRES_IN];
    NSDate *currentDate = [NSDate date];
    
    NSComparisonResult result = [currentDate compare:futherDate];
    /*
     NSOrderedAscending = -1L, NSOrderedSame, NSOrderedDescending
     */
    if (result == NSOrderedAscending) {
        
        return NO;
    }
    
    return YES;
}

@end
